//
//  SCSxPickAlertView.m
//  SmartCamera
//
//  Created by 凯朱 on 2024/4/30.
//  Copyright © 2024 jiahe luo. All rights reserved.
//

#import "SCSxTimeRangePickView.h"

@interface SCSxTimeRangePickView()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong)UIPickerView *leftPicker;
@property (nonatomic, strong)UIPickerView *rightPicker;
@property (nonatomic, strong)NSArray *leftDataSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightWidth;
@property (nonatomic, strong)NSArray *rightDataSource;
@end

@implementation SCSxTimeRangePickView
-(void)awakeFromNib{
    [super awakeFromNib];
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    _titleLbl.text = kLocalString(@"Time Select");
    
    if (@available(iOS 15.0, *)) {
        _leftWidth.constant = 120;
        _rightWidth.constant = 120;
    }else{
        _leftWidth.constant = 140;
        _rightWidth.constant = 140;
    }
    [self addPickView];
    //动画
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 0);
    }];
}

-(void)setCurStartSelects:(NSArray *)curStartSelects{
    if (!curStartSelects) {
        return;
    }
    _curStartSelects = curStartSelects;
    [self defaultSelect:_curStartSelects picker:_leftPicker dataSource:_leftDataSource];
}

-(void)setCurEndSelects:(NSArray *)curEndSelects{
    if (!curEndSelects) {
        return;
    }
    _curEndSelects = curEndSelects;
    [self defaultSelect:_curEndSelects picker:_rightPicker dataSource:_rightDataSource];
}

-(void)defaultSelect:(NSArray *)curSelects picker:(UIPickerView *)picker dataSource:(NSArray *)dataSource{
    [curSelects enumerateObjectsUsingBlock:^(NSString *selectStr, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *sections = dataSource[idx];
        if ([sections containsObject:selectStr]) {
            NSInteger selectRow = [sections indexOfObject:selectStr];
            [picker selectRow:selectRow inComponent:idx animated:NO];
        }
    }];
    [picker reloadAllComponents];
}

-(void)addPickView{
    _leftPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, _leftView.width, _leftView.height)];
    _leftPicker.delegate = self;
    _leftPicker.dataSource = self;
    _leftPicker.backgroundColor = [UIColor clearColor];
    [_leftView addSubview:_leftPicker];
    [_leftPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    _rightPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, _rightView.width, _rightView.height)];
    _rightPicker.delegate = self;
    _rightPicker.dataSource = self;
    _rightPicker.backgroundColor = [UIColor clearColor];
    [_rightView addSubview:_rightPicker];
    [_rightPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    NSMutableArray *hours = [NSMutableArray new];
    NSMutableArray *mines = [NSMutableArray new];
    for (int i = 0; i < 60; i ++) {
        if (i < 24){
            if (i < 10) {
                [hours addObject:[NSString stringWithFormat:@"0%d",i]];
            }else{
                [hours addObject:[NSString stringWithFormat:@"%d",i]];
            }
        }
        if (i < 10) {
            [mines addObject:[NSString stringWithFormat:@"0%d",i]];
        }else{
            [mines addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    _leftDataSource = @[hours,mines];
    _rightDataSource = @[hours,mines];
    [_leftPicker reloadAllComponents];
    [_rightPicker reloadAllComponents];
}


-(IBAction)btnAction:(id)sender{
    if (sender == _cancelBtn) {
        if (_btnBlock) _btnBlock(0,nil,nil);
    }else if (sender == _sureBtn){
        __block NSMutableArray *leftSelects = [NSMutableArray new];
        [_leftDataSource enumerateObjectsUsingBlock:^(NSArray *sections, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger selectedRow = [_leftPicker selectedRowInComponent:idx];
            NSString *value = sections[selectedRow];
            [leftSelects addObject:value];
        }];
        
        __block NSMutableArray *rightSelects = [NSMutableArray new];
        [_rightDataSource enumerateObjectsUsingBlock:^(NSArray *sections, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger selectedRow = [_rightPicker selectedRowInComponent:idx];
            NSString *value = sections[selectedRow];
            [rightSelects addObject:value];
        }];
        
        NSString *startDate = [leftSelects componentsJoinedByString:@":"];
        NSString *endDate = [rightSelects componentsJoinedByString:@":"];
        BOOL isLater = [[NSDate dateWithString:startDate format:@"HH:mm"] isLaterThanDate:[NSDate dateWithString:endDate format:@"HH:mm"]];
        if (isLater || [startDate isEqualToString:endDate]) {
            kShowToast(@"The end time must be later than the start time.");
            return;
        }
        if (_btnBlock) _btnBlock(1,leftSelects,rightSelects);
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
    if (pickerView == _leftPicker) {
        return _leftDataSource.count;
    }
    return _rightDataSource.count;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (pickerView == _leftPicker) {
        NSArray *section = _leftDataSource[component];
        return section.count;
    }
    NSArray *section = _rightDataSource[component];
    return section.count;
}


-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (pickerView == _leftPicker) {
        return _leftPicker.width/2;
    }
    return _rightPicker.width/2;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    if (!view){
        view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
    }
    UILabel *pickerLabel = [view viewWithTag:1000];
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, _leftPicker.width/2-10, 40)];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        pickerLabel.tag = 1000;
        [view addSubview:pickerLabel];
    }
    NSArray *section = nil;
    if (pickerView == _leftPicker) {
        section = _leftDataSource[component];
    }else{
        section = _rightDataSource[component];
    }
    NSString *unit = component == 0 ? kLocalString(@"") : kLocalString(@"");
    NSInteger selectedRow = [pickerView selectedRowInComponent:component];
    NSAttributedString *textStr = [self timeStrWith:section[row] unit:unit isSelect:row == selectedRow];
    pickerLabel.attributedText = textStr;
    return view;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSInteger selectedRow = [pickerView selectedRowInComponent:component];
    UIView *rowView = [pickerView viewForRow:row forComponent:component];
    UILabel *pickerLabel = [rowView viewWithTag:1000];
    NSArray *section = nil;
    if (pickerView == _leftPicker) {
        section = _leftDataSource[component];
    }else{
        section = _rightDataSource[component];
    }
    NSString *unit = component == 0 ? kLocalString(@"") : kLocalString(@"");
    if ([pickerLabel isKindOfClass:[UILabel class]]) {
        NSAttributedString *textStr = [self timeStrWith:section[row] unit:unit isSelect:row == selectedRow];
        pickerLabel.attributedText = textStr;
    }
}

-(NSMutableAttributedString *)timeStrWith:(NSString *)time unit:(NSString *)unit isSelect:(BOOL)isSelect{
    NSDictionary *atrrDict1 = @{NSForegroundColorAttributeName:KWhiteColor_text,NSFontAttributeName:kFont(22)};
    NSDictionary *atrrDict2 = @{NSForegroundColorAttributeName:KWhiteColor_text,NSFontAttributeName:kFont(11)};
    NSDictionary *atrrDict3 = @{NSForegroundColorAttributeName:kHexColor(@"#D5D5D5"),NSFontAttributeName:kFont(20)};
    
    if (isSelect) {
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:time attributes:atrrDict1];
        NSMutableAttributedString *attrUnit = [[NSMutableAttributedString alloc] initWithString:unit attributes:atrrDict2];
        [attr appendAttributedString:attrUnit];
        return attr;
    }else{
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:time attributes:atrrDict3];
        return attr;
    }
}

@end
